﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class Ball : MonoBehaviour
{
    private Vector3 _initPos;
    private Rigidbody2D _rigid;
    private Collider2D _collider;
    private void Awake()
    {
        _initPos = transform.position;
        _rigid = GetComponent<Rigidbody2D>();
        _collider = GetComponent<Collider2D>();
    }

    public void ResetBall()
    {
        transform.position = _initPos;
        GetComponent<Rigidbody2D>().velocity = Vector2.zero;

        Parameter parameter = GameMgr.Instance.parameter;
        _rigid.gravityScale = parameter.gScale;
        _rigid.drag = parameter.linearDrag;
        _rigid.velocity = parameter.velocity * Vector2.left;
        _collider.sharedMaterial.bounciness = parameter.bounciness;
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Target")
        {
            Debug.Log(collision.gameObject.name);
            if (GameMgr.Instance.playMode)
            {
                UnityEngine.Random.InitState(GameMgr.Instance.parameter.seed);
                this.ResetBall();
            }
            else
            {
                Result result = new Result { targetId = collision.name };
                GameMgr.Instance.CollectResult(result);
                GameMgr.Instance.ResetAll();
            }
        }
    }
}